package com.yanqu.road.dao.impl.activity.jiuzhou.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouPatronsData;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouUserData;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouUserRoundRecordData;
import com.yanqu.road.entity.activity.jiuzhou.game.CrossJiuZhouGameRoundRecordData;
import com.yanqu.road.entity.activity.jiuzhou.game.CrossJiuZhouGameRoundRecordUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossJiuZhouGameRoundRecordDataDaoImpl extends TempDataObjectDao<CrossJiuZhouGameRoundRecordData> {
    
    public static String getCrossJiuZhouGameRoundRecordDataSql = "select * from `t_c_jiu_zhou_game_round_record` where `activityId`=? ";

    public Map<Integer, List<CrossJiuZhouGameRoundRecordData>> getCrossJiuZhouGameRoundRecordDataMap(int activityId) {
        Map<Integer, List<CrossJiuZhouGameRoundRecordData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouGameRoundRecordDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJiuZhouGameRoundRecordData data = parse(rs);
                List<CrossJiuZhouGameRoundRecordData> list = map.getOrDefault(data.getRoomId(), new ArrayList<>());
                list.add(data);
                map.put(data.getRoomId(), list);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    public static String insertCrossJiuZhouGameRoundRecordDataSql = "insert into `t_c_jiu_zhou_game_round_record`(activityId,roomId,round,gameRound,user1,user2) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossJiuZhouGameRoundRecordData crossJiuZhouGameRoundRecordData) {
        crossJiuZhouGameRoundRecordData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouGameRoundRecordData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouGameRoundRecordData.getRoomId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouGameRoundRecordData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouGameRoundRecordData.getGameRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGameRoundRecordData.getUser1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouGameRoundRecordData.getUser2())));
        if (getDb().executeNoneQuery(insertCrossJiuZhouGameRoundRecordDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected boolean updateData(CrossJiuZhouGameRoundRecordData data) {
        return false;
    }

    @Override
    protected CrossJiuZhouGameRoundRecordData parseData(ResultSet rs) throws SQLException {
        CrossJiuZhouGameRoundRecordData data = new CrossJiuZhouGameRoundRecordData();
        data.setActivityId(rs.getInt("activityId"));
        data.setRoomId(rs.getInt("roomId"));
        data.setRound(rs.getInt("round"));
        data.setGameRound(rs.getInt("gameRound"));
        String user1 = rs.getString("user1");
        if (!StringUtils.isNullOrEmpty(user1)) {
            data.setUser1(JSON.parseObject(user1, new TypeReference<CrossJiuZhouGameRoundRecordUserData>() {
            }));
        }
        String user2 = rs.getString("user2");
        if (!StringUtils.isNullOrEmpty(user2)) {
            data.setUser2(JSON.parseObject(user2, new TypeReference<CrossJiuZhouGameRoundRecordUserData>() {
            }));
        }
        return data;
    }
}
